//
//  ViewController.swift
//  test
//
//  Created by admin on 2016/11/30.
//  Copyright © 2016年 Zhongshan Camry Electronic Company Limited. All rights reserved.
//

let webview = UIWebView()
let progressProxy = NJKWebViewProgress()
let progressView = NJKWebViewProgressView()

import UIKit

class ViewController: UIViewController,UIScrollViewDelegate,NJKWebViewProgressDelegate,UIWebViewDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        webview.frame = CGRect(x: 0, y: -64, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height + 64)
        self.view.addSubview(webview)
        webview.delegate = progressProxy
        progressProxy.progressDelegate = self
        progressProxy.webViewProxyDelegate = self
        let progressBarHeight:CGFloat = 2.0
        let navigationBarBounds = self.navigationController?.navigationBar.bounds
        let barFrame = CGRect(x: 0, y: (navigationBarBounds?.size.height)! - progressBarHeight, width: (navigationBarBounds?.size.width)!, height: progressBarHeight)
        progressView.frame = barFrame
        progressView.autoresizingMask = .flexibleWidth
        progressView.autoresizingMask = .flexibleTopMargin
        webview.scrollView.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.new, context: nil)
        let url = NSURL(string: "https://www.apple.com/cn")
        let request = NSURLRequest(url: url as! URL)
        print(request)
        webview.loadRequest(request as URLRequest)
        
        let history = UIButton(type:.custom)
        history.setImage(UIImage(named:"10"), for:.normal)
        history.sizeToFit()
        let historyButton:UIBarButtonItem = UIBarButtonItem(customView: history)
        self.navigationItem.leftBarButtonItem = historyButton
        
        let share = UIButton(type:.custom)
        share.setImage(UIImage(named:"11"), for:.normal)
        share.sizeToFit()
        let shareButton:UIBarButtonItem = UIBarButtonItem(customView: share)
        share.addTarget(self, action:#selector(reload), for: .touchUpInside)
        self.navigationItem.rightBarButtonItem = shareButton
        // Do any additional setup after loading the view.
    }
    func reload() {
        webview.reload()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.addSubview(progressView)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        progressView.removeFromSuperview()
    }
    
    func webViewProgress(_ webViewProgress: NJKWebViewProgress!, updateProgress progress: Float) {
        progressView.setProgress(progress, animated: true)
        self.title = webview.stringByEvaluatingJavaScript(from: "document.title")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    /// 方法二：KOV
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        let y = webview.scrollView.contentOffset.y
        if keyPath == "contentOffset" {
            if y < -64 {
                let image = UIImage.creatImage(color: UIColor(red: 1.0, green: 0, blue: 0, alpha: 0), size: CGSize(width: 1, height: 1))
                self.navigationController?.navigationBar.setBackgroundImage(image, for: .default)
            }else if y <= 0 && y >= -64 {
                let image = UIImage.creatImage(color: UIColor(red: 1.0, green: 0, blue: 0, alpha: abs((y + 64)/64)), size: CGSize(width: 1, height: 1))
                self.navigationController?.navigationBar.setBackgroundImage(image, for: .default)
            }
        }
        
    }
    /// 方法一：UIScrollView代理方法
    ///
    /// 
    /*
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print(webview.scrollView.contentOffset.y)
        if webview.scrollView.contentOffset.y <= -64 {
            let image = UIImage.creatImage(color: UIColor(red: 1.0, green: 0, blue: 0, alpha: 0), size: CGSize(width: 1, height: 1))
            self.navigationController?.navigationBar.setBackgroundImage(image, for: .default)
        }else if webview.scrollView.contentOffset.y < 0 && webview.scrollView.contentOffset.y > -64 {
            let image = UIImage.creatImage(color: UIColor(red: 1.0, green: 0, blue: 0, alpha: abs((webview.scrollView.contentOffset.y + 64)/64)), size: CGSize(width: 1, height: 1))
            self.navigationController?.navigationBar.setBackgroundImage(image, for: .default)
        }
//        abs(scroll.contentOffset.y/64)
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
